/**
 * User: c3t
 * Date: 14-2-5
 * Time: 下午3:53
 */
define(function (require, exports, module) {
    var $ = require('js/lib/jquery');
    require('js/lib/core/jquery.ztree.core_3.5')($);
    require('js/lib/core/jquery.ztree.excheck_3.5')($);
    (function ($) {
        $.fn.mytree = function (opts) {
            var setting = $.extend({
                data:{
                    simpleData:{
                        enable: true,
                        idKey: "id",
                        pIdKey: "pid",
                        rootPId: -1
                    }
                },
                async: {
                     enable: true,
                     url: opts ? (opts.url || "admin/channel/treeAll") : "admin/channel/treeAll"
                   /*  autoParam: ["id=pid"]  //?id=* 重命名为?pid=  用于异步加载 **/
                     },
                    view: {
                        dblClickExpand: false,
                        selectedMulti: false,
                        showLine: false
                    },
                    mine: {
                        listChild: 1,
                        srcE: "#cc"
                    }
                }, opts || {});
            var _mine = setting.mine;
            var treeObj = $.fn.zTree.init(this, setting);
            //给ztree对象添加自己定义的方法
            //得到某个节点的父节点
            function getCheckParentNodes(treeNode, checked) {
                var ps = [];
                var pn;
                while ((pn = treeNode.getParentNode())) {
                    if (pn.checked == checked) {
                        ps.push(pn);
                    }
                    treeNode = pn;
                }
                return ps;
            }

            //得到某个节点的所有子节点
            function getCheckChildrenNodes(treeNode, checked, cs) {
                var ccs;
                if (ccs = treeNode.children) {
                    for (var i = 0; i < ccs.length; i++) {
                        var c = ccs[i];
                        if (c.checked == checked) {
                            cs.push(c);
                        }
                        arguments.callee(c, checked, cs);
                    }

                }
            }
            treeObj.getCheckParentNodes=getCheckParentNodes;
            treeObj.getCheckChildrenNodes=getCheckChildrenNodes;
            if (_mine.listChild) {
                treeObj.setting.callback.onClick = listChild;
            }




            function listChild(event, treeId, treeNode) {
                $(_mine.srcE).attr("src", "admin/channel/channels/" + treeNode.id);
            }

            return treeObj;

        }
    })($);

});
